package com.example.shopgateway.predicate;


import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.apache.commons.lang.StringUtils;

import java.util.*;
import java.util.function.Predicate;

@Component
public class NameRoutePredicateFactory extends
        AbstractRoutePredicateFactory<NameRoutePredicateConfig> {
    public NameRoutePredicateFactory() {
        super(NameRoutePredicateConfig.class);
    }
    @Override
    public Predicate<ServerWebExchange> apply(NameRoutePredicateConfig config) {
        return (serverWebExchange)->{
            String name =
                    serverWebExchange.getRequest().getQueryParams().getFirst("name");
            if (StringUtils.isEmpty(name)){
                name = "";
            }
            return name.equals(config.getName());
        };
    }
    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("name");
    }
}
